#
# Localized Starting options ViSa,
# 
# The Team of Visa thanks Lopez for support, modding, helping the civ community
# 

from CvPythonExtensions import *
import Popup as PyPopup

import CvUtil
import CvEventManager
import sys
import PyHelpers
import CvConfigParser

			
gc = CyGlobalContext()

#########################################################################
################### DO NOT DELETE!CRITICAL!!! ###########################
#########################################################################
# minimum distance between cities, default is 3
g_setCityMinDistanceFound = 3
# number of naturally Spread religions, default vanilla setting is 1
g_setReligionNaturalSpreadNumber = 1
# Change the value to false if the religion of the inquisitor should not be 
# required to be present at the city where an inquisition should be started.
# Default value is True
g_bRequireStateReligionForReligionRemoval = True
# Change the value to false if the state religion of the owner of the target 
# city should not match the state religion of the owner of the inquisitor unit 
# to start an inquisition at the target city.
# Default value is True
g_bRequireCityOwnerStateReligionMatchForReligionRemoval = True
# Change the value to false if inquisitors should be able to remove religions
# from any city they can enter.
# Default value is True
g_bRequireOpenBordersForReligionRemoval = True
#########################################################################
################### DO NOT DELETE!CRITICAL!!! ###########################
#########################################################################


def loadConfigurationData():
	global g_setCityMinDistanceFound
	global g_setReligionNaturalSpreadNumber
	global g_setViSaIniLoaded
	global g_bRequireStateReligionForReligionRemoval
	global g_bRequireCityOwnerStateReligionMatchForReligionRemoval
	global g_bRequireOpenBordersForReligionRemoval

	# MOD COMPONENTS
	global g_compControlLoaded
	global g_setCityGuardEnabled
	global g_setCrusadeWonderEnabled
	global g_trainingNSFTCEnabled
	global g_UnitStatisticsEnabled
	global g_always_WAR
	global g_bTechleakEnable
	global g_bPartisanEnable
	global g_bJZoCFeaturesEnabled
	global g_bGeneticEraEnable
	
	
	config = CvConfigParser.CvConfigParser("ViSa_v3.ini")

	if(config != None):
                ################### DO NOT DELETE!!!!!!!!!!!! ###########################
		g_setViSaIniLoaded = config.getboolean(                 "CONFIG",               "ViSa Ini Loaded",                      False)
		g_setCityMinDistanceFound = config.getint(              "ViSa Ini Tweaks",      "Minimum Distance Between Cities",      3)
		g_setReligionNaturalSpreadNumber = config.getint(       "ViSa Ini Tweaks",      "Religion Natural Spread Number",       1)
		g_bRequireStateReligionForReligionRemoval = config.getboolean(                  "Inquisition Mod",      "Require State Religion For Religion Removal",                          True)
		g_bRequireCityOwnerStateReligionMatchForReligionRemoval = config.getboolean(    "Inquisition Mod",      "Require City Owner State Religion Match For Religion Removal",         True)
		g_bRequireOpenBordersForReligionRemoval = config.getboolean(                    "Inquisition Mod",      "Require Open Borders For Religion Removal",                            True)
		################### DO NOT DELETE!!!!!!!!!!!! ###########################

	config = CvConfigParser.CvConfigParser("ViSa-Component-Control.ini")

	if(config != None):	
		### put here all the mod initializations #####
		g_compControlLoaded = config.getboolean(        "ViSa Component Control", "ViSa Component Control Ini Loaded",          False)
		g_trainingNSFTCEnabled = config.getboolean(     "ViSa Component Control", "GIR Training NSFTC",                         False)
		g_setCityGuardEnabled = config.getboolean(      "ViSa Component Control", "GIR City Guard allowed",                     False)
		g_setCrusadeWonderEnabled = config.getboolean(  "ViSa Component Control", "GIR Crusade Wonder allowed",                 False)
		g_always_WAR = config.getboolean(               "ViSa Component Control", "GIR Always War",                             False)
		g_UnitStatisticsEnabled = config.getboolean(    "ViSa Component Control", "Unit Statistics allowed",                    False)
		g_bTechleakEnable = config.getboolean(          "ViSa Component Control", "Techleak Allowed",                           False)		
		g_bPartisanEnable = config.getboolean(          "ViSa Component Control", "Partisan allowed",                           False)
		g_bJZoCFeaturesEnabled = config.getboolean(     "ViSa Component Control", "JZoC Features allowed",                      False)
		g_bGeneticEraEnable = config.getboolean(        "ViSa Component Control", "Genetic Era",                                False)
		
		
	gc.getGame().setCityMinDistanceFound(g_setCityMinDistanceFound)
	gc.getGame().setReligionNaturalSpreadNumber(g_setReligionNaturalSpreadNumber)
	gc.getGame().setRequireStateReligionForReligionRemoval(g_bRequireStateReligionForReligionRemoval)
	gc.getGame().setRequireCityOwnerStateReligionMatchForReligionRemoval(g_bRequireCityOwnerStateReligionMatchForReligionRemoval)			
	gc.getGame().setRequireOpenBordersForReligionRemoval(g_bRequireOpenBordersForReligionRemoval)
	

	
			
class CvVisaIniTweaks:
	
	def __init__(self, eventManager):

		eventManager.addEventHandler("GameStart", self.onGameStart)

	def onGameStart(self, argsList):
		loadConfigurationData()
		

####################################################################################################
####################################################################################################
####################################################################################################
########################                                                ############################
########################        SECTION BELOW INFORMS STATUS IF         ############################
########################        MOD COMPONENTS ARE INITIALIZED          ############################
########################                                                ############################
####################################################################################################
####################################################################################################
####################################################################################################
                
		# is the ViSa ini file loaded succesfully?
		if ( g_setViSaIniLoaded ):
			message = "ViSa ini was initialized succesfully!"
			CyInterface().addImmediateMessage(message, "")

			if ( g_compControlLoaded ):
				message = "ViSa Component Control ini was initialized sucessfully, please check your log for detailed information about components"
				CyInterface().addImmediateMessage(message, "")


				# tell user if mod is initialized or not
				#
				# Techleak
				#
				if ( g_bTechleakEnable ):
					message = "Techleak Mod Enabled"			
					self._addMessageEnable(message)
				else:
					message = "Techleak Mod Disabled"
					self._addMessageDisable(message)
				#
				# Unit Statistics
				#
				if ( g_UnitStatisticsEnabled ):
					message = "Unit Statistics Mod Enabled"			
					self._addMessageEnable(message)
				else:
					message = "Unit Statistics Mod Disabled"
					self._addMessageDisable(message)
				#
				# Jeckel Zone of Control Forts
				#
				if ( g_bJZoCFeaturesEnabled ):
					message = "Jeckel Zone of Control Forts Mod Enabled"			
					self._addMessageEnable(message)
				else:
					message = "Jeckel Zone of Control Forts Mod Disabled"
					self._addMessageDisable(message)
				#
				# GIR
				#
				# City Guard
				if ( g_setCityGuardEnabled ):
					message = "GIR City Guard Mod Enabled"			
					self._addMessageEnable(message)
				else:
					message = "GIR City Guard Mod Disabled"
					self._addMessageDisable(message)
				# Crusade Wonder
				if ( g_setCrusadeWonderEnabled ):
					message = "GIR Crusade Wonder Mod Enabled"			
					self._addMessageEnable(message)
				else:
					message = "GIR Crusade Wonder Mod Disabled"
					self._addMessageDisable(message)
				# NSFTC
				if ( g_trainingNSFTCEnabled ):
					message = "GIR NSFTC Mod Enabled"			
					self._addMessageEnable(message)
				else:
					message = "GIR NSFTC Mod Disabled"
					self._addMessageDisable(message)
				# Always War
				if ( g_always_WAR ):
					message = "GIR ALWAYS WAR Mod enabled"
					self._addMessageEnable(message)
				else:
					message = "GIR ALWAYS WAR Mod Disabled"
					self._addMessageDisable(message)
				#
				# Partisan
				#
				if ( g_bPartisanEnable ):
					message = "Partisan Mod Enabled"
					self._addMessageEnable(message)
				else:
					message = "Partisan Mod Disabled"
					self._addMessageDisable(message)
				#
				# Genetic Era
				#
				if ( g_bGeneticEraEnable ):
					message = "Genetic Mod Enabled"
					self._addMessageEnable(message)
				else:
					message = "Genetic Mod Disabled"
					self._addMessageDisable(message)
			### here is the exception if there is some problem with the ini file
			else:
				message = "WARNING, the ViSa Mod Control ini wasn't initialized correctly, your mod installation might be corrupted!"
				message = message + " Mod settings have been disabled! We strongly suggest to repair your installation!"
				CyInterface().addImmediateMessage(message, "")	
		### here is the exception if there is some problem with the ini file
		else:
			message = "WARNING, the ViSa ini wasn't initialized correctly, your mod installation might be corrupted!"
			message = message + " Mod settings have been disabled! We strongly suggest to repair your installation!"
			CyInterface().addImmediateMessage(message, "")
			
###################################################################################################################################
###################################################################################################################################
####################                                                                                       ########################
####################                 Prints the message on the screen and event window                     ########################
####################                                                                                       ########################
###################################################################################################################################
###################################################################################################################################

	def _addMessageEnable(self, szString):
		"""Displays an on-screen message."""
		eventMessageTimeLong = gc.getDefineINT("EVENT_MESSAGE_TIME_LONG")
		for iPlayer in range(gc.getMAX_PLAYERS()):
			ePlayer = gc.getPlayer(iPlayer)
			if (ePlayer.isAlive() and ePlayer.isHuman()):
				CyInterface().addMessage(ePlayer.getID(), True, eventMessageTimeLong, szString, None, 0, None, ColorTypes(8), 0, 0, False, False)

	def _addMessageDisable(self, szString):
		"""Displays an on-screen message."""
		eventMessageTimeLong = gc.getDefineINT("EVENT_MESSAGE_TIME_LONG")
		for iPlayer in range(gc.getMAX_PLAYERS()):
			ePlayer = gc.getPlayer(iPlayer)
			if (ePlayer.isAlive() and ePlayer.isHuman()):
				CyInterface().addMessage(ePlayer.getID(), True, eventMessageTimeLong, szString, None, 0, None, ColorTypes(7), 0, 0, False, False)
